<template>
    <view class="box">
		<view  class="guwen" >
			<guwen-item :options="guwen"  ></guwen-item>
		</view>
		<view class="operate cu-list grid col-3 no-border">
			<view class="item">
				<button v-if="isLike"  @tap="like" class="cu-item">
					<view  class="cuIcon-likefill text-blue"></view>
					<view>已点赞</view>
				</button>
				<button v-else @tap="like" class="cu-item">
					<view  class="cuIcon-like text-blue"></view>
					<view>为我点赞</view>
				</button>
				
			</view>
			<view  class="item">
			
				<button class="cu-item" open-type="share" >
					<view class="cuIcon-forward text-blue "></view>
					<view>转发</view>
				</button>
			</view >
			<view  class="item">
				<button v-if="guwen.code!=''" class="cu-item" @tap="preImage" :data-url="guwen.code">
					<view class="cuIcon-qr_code text-blue"></view>
					<view>二维码</view>
				</button>
			</view>
		</view>
		<view class="bottom">
			<button style="background-color:#1AAD19" @tap="goHouse" :data-houseid="guwen.houseid">楼盘详情</button>
			<button style="background-color:#007AFF;" @tap="call" :data-tel="guwen.tel">电话咨询</button>
		</view>
    </view>
</template>
<script>
    import guwenItem from './item.vue';
    export default {
        components: {
            guwenItem
        },
        data() {
            return {
                guwen: {},
				id:0,
				imgHttp:"",
				isLike:false,
             }
        },
        onLoad(e) {
			
			var me=this;
			me.id = e.id ;
			
			me.getDetail();
			
			
			uni.getStorage({
				key: 'zanList',
				success: (res) => {
					
					var zanList=res.data;
					
					var index = zanList.indexOf(me.id);
					if (index > -1) {
						me.isLike=true;
					}
					else
					{
						me.isLike=false;
					}
					
				},
				fail: (e) => {
				}
			});
        },
		onShareAppMessage(){
			return {
			      title: '选楼盘，查佣金，看房价'
			    }
		},
        methods: {
            
            getDetail() {
				var me=this;
				
                uni.request({
                	url: me.$rootUrl+'/Saler/Find',
                	data: {id:me.id},
                	method: 'GET',
                	success(res) {	
                		if (res.data.success) {
                			me.guwen=res.data.data;
                		} else {
                			uni.showModal({
                				content: '服务器错误!',
                				showCancel: false
                			});
                		}
                	},
                	fail() {
                		uni.showModal({
                			content: me.connectionFailText,
                			showCancel: false
                		});
                	}
                });
            },
			
			preImage(e){
				var me=this;
				var root="http://wx.attch.we.qsace.com/";
				if(e.currentTarget.dataset.url.indexOf("Attachment")>=1)
				{
						root="http://www.zhunafc.com";
					
				}
				var url=root+e.currentTarget.dataset.url
				 // 预览图片
				uni.previewImage({
					urls: [url],
					current:0,
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			goHouse(e) {
			    
				var id=e.currentTarget.dataset.houseid
				var url='/pages/new/detail/detail?id='+id;
			    uni.navigateTo({
			        url: url
			    });
			},
			call(e) {
			    
				var tel=e.currentTarget.dataset.tel
				uni.makePhoneCall({
				    phoneNumber: tel 
				});
			},
			like(){
				var me=this;
				if(me.isLike)
				{
					me.guwen.zan--;
				}
				else
				{
					me.guwen.zan++;
				}
				me.isLike=!me.isLike;
				uni.request({
					url: me.$rootUrl+'/Saler/Like',
					data: {id:me.id,IsLike:me.isLike},
					method: 'POST',
					success(res) {	
						if (res.data.success) {
							
							
							uni.getStorage({
								key: 'zanList',
								success: (res) => {
									var zanList=res.data;
									if(me.isLike)
									{
										zanList.push(me.id);
									}
									else
									{
										var index = zanList.indexOf(me.id);
										if (index > -1) {
											zanList.splice(index, 1);
										}
									}
									uni.setStorage({
										key:'zanList',
										data:zanList
									})
								},
								fail: (e) => {
									var zanList=new Array;
									zanList.push(me.id);
									uni.setStorage({
										key:'zanList',
										data:zanList
									})
								}
							});
							
							
							
						} else {
							uni.showModal({
								content: '服务器错误!',
								showCancel: false
							});
						}
					},
					fail() {
						uni.showModal({
							content: me.connectionFailText,
							showCancel: false
						});
					}
				});
			}
		}
    }
</script>

<style lang="scss">
    .box{
		.guwen{
			margin-top:13px;
		}
		.operate{
			display: flex;
			flex-direction: row;
			.item{
				flex:1;
				padding:15px;
				.cu-item{
					line-height:1.1;
					font-size:12px;
					border-radius: 10px;;
					background: #CCCCCC;
					.text-blue{
						font-size: 30px;
					}
					view{
						padding:4px;
					}
				}
			}
		}
		.bottom{
			position: fixed;
			bottom:0;
			display: flex;
			flex-direction: row;
			padding: 10px 0;
			width:100%;
			
			text-align: center;
			
			button{
				font-size: 30upx;
				color:white;
				width:40%;	
			}
		}
	}
</style>
